<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Authentification extends CI_Controller 
{

   public function index()
   {
      // If a user is known, it redirects to profile 
      if($this->session->userdata('USE_LOGIN'))
      {
         redirect('profil');
      }
      else
      {
         // Rules validation for the login form (login and password)
         $this->form_validation->set_rules('login','Identifiant', 'trim|required|xss_clean');
         $this->form_validation->set_rules('password','Mot de passe', 'trim|required|xss_clean');
	
         if ($this->form_validation->run())
         {
            // Loading Model
            $this->load->model('authentification_model');
			
            // We define the parameters users with login (utilisateur_id, societe_id, utilisateur_nom, utilisateur_prenom, utilisateur_email, utilisateur_password, utilisateur_actif) 
            $data = $this->authentification_model->get_authentification();

            if(!empty($data))
            {
               // We apply the settings to the current session
               $this->session->set_userdata($data);

               //$_SESSION['utilisateur_id'] = $data['utilisateur_id'];
               
               
               redirect('Profil');
            }
            else
            {
               
               // Display an error message
               $data = array(
                   'alertType'=>'error',
                   'alertText'=>'Identifiant ou mot de passe incorrect !'
               );
		
               page('authentification', 'Authentification', $data);
            }
         }
         else
         {
            // Initialization of session variables
            $this->session->sess_destroy();

            // Incorrect form, the user must re-enter their username and password
            //$this->template->load('template', 'login');
            page('authentification', 'Authentification', null);
         }
      }
   }
   
   public function Deconnexion()
   {
      $this->session->sess_destroy();
      redirect('');
   }
}

/* End of file main.php */
/* Location: ./application/controllers/main.php */